package com.wbotx.wsystemdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.wbotx.system.iPackageManager;
import com.wbotx.system.iSystem;
import com.wbotx.device.Gpio;
import com.wbotx.wsystem.wSystemManager;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {
    Gpio mGpio;
    wSystemManager mSystemManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSystemManager = wSystemManager.connectService(this, new wSystemManager.wSystemManagerEvent(){

            @Override
            public void onConnected(wSystemManager wSystemManager) {
                Log.e("2400", "wSystemManager onConnected" + wSystemManager);
            }

            @Override
            public void onDisConnected(wSystemManager wSystemManager) {
                Log.e("2400", "onDisConnected onConnected" + wSystemManager);
            }
        });
        findViewById(R.id.enable_setting).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iSystem.execRoot("pm enable com.android.settings/.Settings");
            }
        });
        findViewById(R.id.disable_setting).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iSystem.execRoot("pm disable com.android.settings/.Settings");
            }
        });
        findViewById(R.id.get_gpio).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getGpio();
            }
        });
        findViewById(R.id.set_gpio).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setGpio();
            }
        });
        findViewById(R.id.install).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String path = ((TextView)findViewById(R.id.install_path)).getText().toString();
                boolean block = ((CheckBox)findViewById(R.id.install_block)).isChecked();
                boolean modeTwo = ((CheckBox)findViewById(R.id.install_mode)).isChecked();
                if ( modeTwo )
                    installAppModeTwo(path, block);
                    else
                    installAppModeOne(path, block);
            }
        });
        findViewById(R.id.uninstall).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String packagename = ((TextView)findViewById(R.id.uninstall_packagename)).getText().toString();
                boolean block = ((CheckBox)findViewById(R.id.install_block)).isChecked();
                boolean modeTwo = ((CheckBox)findViewById(R.id.uninstall_mode)).isChecked();
                if ( modeTwo )
                    uninstallAppModeTwo(packagename, block);
                    else
                    uninstallAppModeOne(packagename, block);
            }
        });
        findViewById(R.id.screencap).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String path = ((TextView)findViewById(R.id.screencap_path)).getText().toString();
                boolean block = ((CheckBox)findViewById(R.id.screencap_block)).isChecked();
                screencap(path, block);
            }
        });
        findViewById(R.id.set_time).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    int year = Integer.parseInt(((TextView) findViewById(R.id.year)).getText().toString());
                    int month = Integer.parseInt(((TextView) findViewById(R.id.month)).getText().toString());
                    int day = Integer.parseInt(((TextView) findViewById(R.id.day)).getText().toString());
                    int hour = Integer.parseInt(((TextView) findViewById(R.id.hour)).getText().toString());
                    int minite = Integer.parseInt(((TextView) findViewById(R.id.minite)).getText().toString());
                    int second = Integer.parseInt(((TextView) findViewById(R.id.second)).getText().toString());
                    Calendar calendar = Calendar.getInstance();
                    calendar.set(year, month, day, hour, minite, second);
                    Log.e("2400", "date: " + calendar.getTime());
                    mSystemManager.setTime(calendar.getTimeInMillis());
                }catch (Exception e ){
                    e.printStackTrace();
                }
            }
        });
    }
    void installAppModeTwo(final String path, boolean block){
        int rev = 0;
        if ( block ) {
            Log.e("2400", "packageInstall start: " + path);
            rev = mSystemManager.packageInstall(path);
            Log.e("2400", "packageInstall end: " + path + " rev: " + rev);
        }else {
            rev = mSystemManager.packageInstall(path, new wSystemManager.InstallEvent() {
                @Override
                public void onInstallStart(String s) {
                    Log.e("2400", "onInstallStart path: " + s);
                }

                @Override
                public void onInstallProgress(String s, float v) {
                    Log.e("2400", "onInstallProgress path: " + s + " v: " + v);
                }

                @Override
                public void onInstallSucess(String s) {
                    Log.e("2400", "onInstallSucess path: " + s);
                }

                @Override
                public void onError(String s) {
                    Log.e("2400", "onError path: " + s);
                }
            });
            Log.e("2400", "packageInstall with event end: " + path + " rev: " + rev);
        }
    }

    void uninstallAppModeTwo(final String packagename, boolean block){
        int rev = 0;
        if ( block ) {
            Log.e("2400", "deletePackage start: " + packagename);
            rev = mSystemManager.deletePackage(packagename);
            Log.e("2400", "deletePackage end: " + packagename + " rev: " + rev);
        }else {
            rev = mSystemManager.deletePackage(packagename, new wSystemManager.DeleteAppEvent() {
                @Override
                public void packageDeleted(String packagename, int code) {
                    Log.e("2400", "deletePackage start: " + packagename + " code: " + code);
                }
            });
            Log.e("2400", "deletePackage with event end: " + packagename + " rev: " + rev);
        }
    }
    void screencap(final String path, boolean block){
        if ( block ){
            new Thread(){
                @Override
                public void run() {
                    super.run();
                    Log.e("2400", "screencapBlock start:");
                    int rev = iSystem.screencapBlock(path);
                    Log.e("2400", "screencapBlock rev: " + rev );
                }
            }.start();
        }else{
            int rev = iSystem.screencap(path);
            Log.e("2400", "screencap rev: " + rev );
        }
    }
    void uninstallAppModeOne(final String packagename, boolean block){
        if ( block )
        new Thread(){
            @Override
            public void run() {
                super.run();
                Log.e("2400", "uninstallAppBlock start:");
                int rev = iPackageManager.uninstallAppBlock(packagename);
                Log.e("2400", "uninstallAppBlock rev: " + rev);
            }
        }.start();
        else {
            int rev = iPackageManager.uninstallApp(packagename);
            Log.e("2400", "uninstallApp rev: " + rev);
        }
    }

    void installAppModeOne(final String filename, boolean block){
        if ( block ){
            new Thread(){
                @Override
                public void run() {
                    super.run();
                    Log.e("2400", "installAppBlock start:");
                    int rev = iPackageManager.installAppBlock(filename);
                    Log.e("2400", "installAppBlock rev: " + rev);
                }
            }.start();
        }else {
            int rev = iPackageManager.installApp(filename);
            Log.e("2400", "installApp rev: " + rev);
        }
    }
    void requestGpio(){
        int gpioid = -1;
        String gpiostr = ((TextView)findViewById(R.id.gpio_id)).getText().toString();
        if( gpiostr == null || gpiostr.length() <= 0 )
            return;
        gpioid = Integer.parseInt(gpiostr);
        if ( null == mGpio || gpioid != mGpio.getGpio() ){
            if ( null != mGpio )
                mGpio.free();
            mGpio = new Gpio(gpioid);
            mGpio.request();
        }
    }
    void setGpio(){
        if ( null == mGpio )
            requestGpio();
        if ( null != mGpio ) {
            String gpiovalueStr = ((TextView)findViewById(R.id.set_gpio_value)).getText().toString();
            if ( gpiovalueStr == null || gpiovalueStr.length() <= 0 )
                return;
            mGpio.set(Integer.parseInt(gpiovalueStr));
        }
    }

    int getGpio(){
        if ( null == mGpio )
            requestGpio();
        if ( null != mGpio ) {
            ((TextView)findViewById(R.id.gpio_value)).setText("" + mGpio.get());
        }
        return -1;
    }
    void freeGpio(){
        if ( mGpio != null )
            mGpio.free();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        freeGpio();
        mSystemManager.disConnect();

    }
}